package ema.tpjee.banking.test;

import java.util.Collection;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;

import test.TestFacadeRemote;
import ema.tpjee.banking.model.Bank;
import ema.tpjee.banking.model.City;
import ema.tpjee.banking.model.Customer;

public final class BankingClient {

	public static void main(final String[] args) throws Exception {

		System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory");
		System.setProperty(Context.PROVIDER_URL, "localhost:1099");
		System.setProperty(Context.URL_PKG_PREFIXES,
				"org.jboss.naming:org.jnp.interfaces");

		System.out.println("lookup TestFacade/remote ");
		
		TestFacadeRemote facadeBean = (TestFacadeRemote) new InitialContext()
				.lookup("TestFacade/remote");

		// Initialisation de la base
		facadeBean.populate();
		
		
		
	    System.out.println("Liste des communes");
		
		List<City> cities = facadeBean.getCities();
		if (cities!=null){
			for (City city : cities) {
				System.out.println(city);
			}
		}
		
		facadeBean.delete(facadeBean.getCities().get(1));
		
		System.out.println("Liste des communes après la suppression de la seconde");
		
		cities = facadeBean.getCities();
		if (cities!=null){
			for (City city : cities) {
				System.out.println(city);
			}
		}		
		
		
		

		List<Bank> banks = facadeBean.getBanks();
		if (banks != null) {
			for (Bank bank : banks) {
				System.out.println("Liste des clients pour la banque "
						+ bank.getName() + " :");
				Collection<Customer> customers = bank.getCustomers();
				if (customers == null) {
					System.out.println(" Aucun client");
				} else {
					for (Customer customer : customers)
						System.out.println("    client: " + customer);
				}
			}
		} else {
			System.out.println("Aucune banque");
		}
		// Recuperation de la liste des Customers
		System.out.println("");
		System.out.println("Liste des clients + banque :");
		List<Customer> customers = facadeBean.getCustomers();
		if (customers != null) {
			facadeBean.getCustomers().get(0).setCity(facadeBean.getCities().get(0));
			facadeBean.save(facadeBean.getCustomers().get(0));
			for (Customer customer : customers) {
				System.out.println("client: " + customer.toString()
						+ " /banque: " + customer.getBank().getName());
			}
		}

		
		// suppression d'un client
		
		try {
			Customer del = facadeBean.getCustomers().get(1);
			System.out.println("suppression du client " + del);
			facadeBean.delete(del);
		} catch (Exception e) {
			System.out.println("erreur pendant la suppression du client");
		}
	
		
		System.out.println("Liste des clients après la suppression");
		customers = facadeBean.getCustomers();
		if (customers != null) {
			for (Customer Customer : customers) {
				System.out.println(Customer.getName());
			}
		}
		
		// suppression d'une banque

		Bank toDelete = facadeBean.getBanks().get(1);
		System.out.println("suppression de la banque " + toDelete);
		facadeBean.delete(toDelete);

		System.out.println("Liste des banques après la suppression");
		banks = facadeBean.getBanks();
		if (banks != null) {
			for (Bank bank : banks)
				System.out.println(bank.getName());

		}
		
		
		System.out.println("Liste des clients après la suppression de la banque");
		customers = facadeBean.getCustomers();
		if (customers != null) {
			for (Customer Customer : customers) {
				System.out.println(Customer.getName());
			}
		}
		
	
		
		
	}
}
